/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.ppm.dat.view;

import bgu.ppm.dat.model.Table;
import bgu.ppm.dat.view.PivotTableWorkbunchView.TableVisualizationFactory;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Image;
import javax.swing.JComponent;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategorySeriesLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;

/**
 *
 * @author User
 */
public class PivotBarChartView extends javax.swing.JPanel implements TableVisualizationFactory {

    /**
     * Creates new form PivotLineChartView
     */
    public PivotBarChartView() {
        initComponents();
    }

    private JFreeChart createChart(CategoryDataset categorydataset) {
//        JFreeChart jfreechart = ChartFactory.createLineChart("Line Chart Demo 8", "Category", "Count", categorydataset, PlotOrientation.VERTICAL, true, true, false);
//        jfreechart.setBackgroundPaint(Color.white);
//        CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
//        categoryplot.setBackgroundPaint(Color.lightGray);
//        categoryplot.setRangeGridlinePaint(Color.white);
//        LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
//        for (int i = 0; i < categorydataset.getRowCount(); i++) {
//            lineandshaperenderer.setSeriesShape(i, ShapeUtilities.createDiamond(4F));
//            lineandshaperenderer.setSeriesShapesVisible(i, true);
//        }
//        lineandshaperenderer.setDrawOutlines(true);
//        lineandshaperenderer.setUseFillPaint(true);
//        lineandshaperenderer.setBaseFillPaint(Color.white);
//        return jfreechart;

        JFreeChart jfreechart = ChartFactory.createBarChart("Bar Chart Demo", "Category", "Value", categorydataset, PlotOrientation.VERTICAL, true, true, false);
        jfreechart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
        barrenderer.setDrawBarOutline(false);
        barrenderer.setMaximumBarWidth(0.10000000000000001D);
        barrenderer.setItemMargin(0.1D);
        barrenderer.setLegendItemLabelGenerator(new StandardCategorySeriesLabelGenerator("{0} series"));
//        GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, Color.lightGray);
//        GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, Color.lightGray);
//        barrenderer.setSeriesPaint(0, gradientpaint);
//        barrenderer.setSeriesPaint(1, gradientpaint1);
        return jfreechart;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    @Override
    public JComponent getComponent(Table table) {
        removeAll();

        add(new ChartPanel(createChart(DatasetUtils.createDataset(table))), BorderLayout.CENTER);

        return this;
    }

    @Override
    public String getVisualName() {
        return "Line chart";
    }

    @Override
    public Image getIcon() {
        return null;
    }

    @Override
    public String getDescription() {
        return "Line chart";
    }
}
